package com.zwq.controller;

import com.zwq.pojo.dto.UserDto;
import com.zwq.pojo.request.user.*;
import com.zwq.service.UserService;
import com.zwq.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/login/password")
    public ResponseResult<UserDto> loginByPassword(@RequestBody UserPasswordLoginRequest request) {
        return ResponseResult.success(userService.loginByPassword(request.getUsername(), request.getPassword()));
    }

    @PostMapping("/login/email")
    public ResponseResult<UserDto>  loginByEmail(@RequestBody UserEmailLoginRequest request) {
        return ResponseResult.success(userService.loginByEmail(request.getEmail(), request.getCode()));
    }

    @GetMapping("/wechat-qr")
    public ResponseResult<Map<String, String>> getWechatQr() {
        return ResponseResult.success(userService.getWechatQr());
    }

    @PostMapping("/wechat-login-check")
    public ResponseResult<UserDto> checkWechatLogin(@RequestParam String code, @RequestParam String state) {
        return ResponseResult.success(userService.checkWechatLogin(code, state));
    }

    @PostMapping("/send-email-code")
    public ResponseResult<?> sendEmailCode(@RequestBody SendEmailCodeRequest request) {
        userService.sendEmailCode(request.getEmail());
        return ResponseResult.success("验证码已发送");
    }

    @PostMapping("/register")
    public ResponseResult<UserDto> register(@RequestBody UserRegisterRequest request) {
        return ResponseResult.success(userService.register(request));
    }

    // 获取个人信息
    @GetMapping("/profile")
    public ResponseResult<UserDto> getProfile(@RequestParam Long userId) { // 实际应从认证信息中获取
        return ResponseResult.success(userService.getProfile(userId));
    }

    // 更新个人信息
    @PutMapping("/profile")
    public ResponseResult<UserDto> updateProperty(@RequestParam Long userId, @RequestBody UserDto userDto) { // 实际应从认证信息中获取 userId
        return ResponseResult.success(userService.updateProperty(userId, userDto));
    }
}